/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.oa.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.web.HttpOutMessage;
import com.thinkgem.jeesite.common.web.OutMessageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.oa.entity.Subject;
import com.thinkgem.jeesite.modules.oa.service.SubjectService;

import java.util.List;

/**
 * 科目Controller
 * @author Michael
 * @version 2017-07-31
 */
@Controller
@RequestMapping(value = "${adminPath}/oa/subject")
public class SubjectController extends BaseController {

	@Autowired
	private SubjectService subjectService;
	
	@ModelAttribute
	public Subject get(@RequestParam(required=false) String id) {
		Subject entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = subjectService.get(id);
		}
		if (entity == null){
			entity = new Subject();
		}
		return entity;
	}
	
	@RequiresPermissions("oa:subject:view")
	@RequestMapping(value = {"list", ""})
	public String list(Subject subject, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Subject> page = subjectService.findPage(new Page<Subject>(request, response), subject); 
		model.addAttribute("page", page);
		return "modules/oa/subjectList";
	}
	@RequestMapping(value = {"subjectsJson"})
	@ResponseBody
	public HttpOutMessage subjectsJson(Subject subject, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Subject> subjectList = subjectService.findList(new Subject());
		HttpOutMessage httpOutMessage =OutMessageUtils.buildOutMessage(HttpOutMessage.SUCCESS,subjectList);
		return httpOutMessage;
	}
	@RequiresPermissions("oa:subject:view")
	@RequestMapping(value = "form")
	public String form(Subject subject, Model model) {
		model.addAttribute("subject", subject);
		return "modules/oa/subjectForm";
	}

	@RequiresPermissions("oa:subject:edit")
	@RequestMapping(value = "save")
	public String save(Subject subject, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, subject)){
			return form(subject, model);
		}
		subjectService.save(subject);
		addMessage(redirectAttributes, "保存科目成功");
		return "redirect:"+Global.getAdminPath()+"/oa/subject/?repage";
	}
	
	@RequiresPermissions("oa:subject:edit")
	@RequestMapping(value = "delete")
	public String delete(Subject subject, RedirectAttributes redirectAttributes) {
		subjectService.delete(subject);
		addMessage(redirectAttributes, "删除科目成功");
		return "redirect:"+Global.getAdminPath()+"/oa/subject/?repage";
	}

}